import java.util.Arrays;

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {
    public static void main(String[] args) {

    }



    public static void main7(String[] args) {    //去掉左右两边的空格
        String str = "hello hello      ";
        String ret = str.trim();
        System.out.println(ret);
        boolean flg = str.contains("hello");
        System.out.println(flg);
        System.out.println(str.endsWith("  "));   //是否以这个结尾/开头
    }


    public static void main6(String[] args) {    //字符串截取
        String str = "helloworld";
        String ret1 = str.substring(1);
        System.out.println(ret1);
        String ret2 = str.substring(1,4); //左闭右开[1,4)   打印 ell
        System.out.println(ret2);
    }

    public static void main5(String[] args) {   //字符串拆分
        String str = "zhangsan&wangwu&lisi";
        String[] ret = str.split("&",2);  //至多分2组
        System.out.println(Arrays.toString(ret));  //[zhangsan, wangwu&lisi]
        String str2 = "192.168.1.2";
        String[] ret2 = str2.split("\\.");    //转义| * + . 要写两个\
        for(String x:ret2) {
            System.out.println(x);
        }
    }


    public static void main4(String[] args) {   //字符串替换
        String str1 = "abcdeabdabc";
        String ret1 = str1.replace('a','q');   //返回的是新的字符串，没有修改原来的
        System.out.println(ret1);
        String ret2 = str1.replace("ab","qq");
        System.out.println(ret2);

    }


    public static void main3(String[] args) {   //字符串转化
        String str = String.valueOf(1234);    //String.valueOf() :把int转化成String
        String s4 = String.valueOf(new Student("Hana",19));   //调用toString,没重写就调用obj的
        System.out.println(s4);

        int val1 = Integer.parseInt("123");   //Integer.parseInt():把string转成int
        int val2 = Integer.valueOf("123");   //用得少，但也可以
        System.out.println(val1+1);

        String str1 = "hello";    //大小写转化
        System.out.println(str1.toUpperCase());
        System.out.println("===========");

        char[] chars = str1.toCharArray();  //字符串转化成数组
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }
        String s2 = new String(chars);  //数组转字符串

        String str4 = String.format("%d-%d-%d",2012,12,12);

    }


    public static void main2(String[] args) {   //字符串查找
        String str = "abcdcf";
//        for (int i = 0; i < str.length(); i++) {
//            char ch = str.charAt(i);
//            System.out.println(ch);
//        }
        System.out.println(str.indexOf('c'));  //从头开始找第一个就结束
        System.out.println(str.indexOf('c',3)); //从下标为3开始找到就结束
        System.out.println(str.indexOf("dc"));  //从头开始找字符串，返回第一个第下标
        System.out.println(str.lastIndexOf('c'));  //从尾开始找
        System.out.println(str.lastIndexOf("dc",3));  //从3开始往回找(只要首字符在范围内就行，后面的超出也无所谓)
    }



    public static void main1(String[] args) {    //初始化和比较
        //三种构造方法
        String str1 = "hello";
        System.out.println(str1.length());
        String str2 = new String("abc");
        System.out.println(str1.equals(str2));  //比较相等

        int ret = str1.compareTo(str2);   //比较大小 出现不等的直接返回它俩大小差值，相同输出0，前k个都相同返回长度差值
        System.out.println(ret);   //str1-str2 = 7


        char[] chars = {'a','b','c'};
        String str3 = new String(chars);
    }
}
